/*
 * @Author: 89-83-77
 * @Description: 用户信息存储（登录-登出）
 * @Date: 2025-03-27 17:40:44
 * @LastEditTime: 2025-03-28 11:41:43
 * @FilePath: \fz-yzt-webd:\work\client\src\stores\user.js
 */
import { defineStore } from "pinia";

// 用户信息存储
export const useUserStore = defineStore("user", {
  state: () => ({
    // 用户信息
    role: "admin",
    token: localStorage.getItem("token") || "",
  }),
  actions: {
    // 设置用户信息
    setUserInfo(data) {
      this.role = data.role;
      this.token = data.token;
      localStorage.setItem("token", data.token);
    },
    // 清除用户信息
    clearUserInfo() {
      this.role = "";
      this.token = "";
      localStorage.removeItem("token");
    },
  },
  getters: {
    // 权限验证
    hasPermission: (state) => (requiredRoles) => {
      if (!requiredRoles || requiredRoles.length === 0) return true;
      return requiredRoles.includes(state.role);
    },
  },
});
